//
//  NetTools.swift
//  SinaWb
//
//  Created by Jiang on 16/5/12.
//  Copyright © 2016年 Jiang. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD


class ViewModel {
    //一些要使用的参数
//   static let UserInfo : UserModel? = UserModel.GetTheUserMode() //只会实例化一次UerMode,但不会被释放,在dismissLoginView的回调中不能改变UserInfo,依然会是nil
    static  let sharViewModel  = ViewModel()
    
    var UserInfo :UserModel?
    
    var userLogin : Bool {
        return UserInfo != nil
    }
    
    var accese_taken:String?{
        
        return UserInfo?.access_token
    }
    
    private  init() {
        
        UserInfo = UserModel.GetTheUserMode()
    }
    //MARK: - 从服务器的JSON数据加载四个TabBarItem
    class  func GetTheMainTabBarController()->(UIViewController){
        //   读取JSON数据中使用的key
        let  jsonControler = "vcName" ,jsontitle = "title" ,jsonimage = "imageName"
        
        let  imageArray = ["tabbar_home","tabbar_message_center","tabbar_discover","tabbar_profile"]
        let  TitleArray = ["首页","消息","发现","我"]
        let  controllerArray = ["JkmHomeTableViewController",
                                "JkmMessageTableViewController",
                                "JkmDiscoverTableViewController",
                                "JkmProfileTableViewController" ]
        
        //-----

        
        let defaultMainTabBar = JkmMainTabBarController(imageArray: imageArray, TitleArray: TitleArray, controllerArray: controllerArray)
        
        let JsonPath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
       //------
        
        guard JsonPath != nil else{
            JkLog("Json文件不存在或者文件名错误")
            return defaultMainTabBar
        }
        
        guard  let data = NSData(contentsOfFile: JsonPath!) else{
            JkLog("Json文件为空")
            return defaultMainTabBar
        }
        
        do{
            
            let objec = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! Array<[String:AnyObject]>
            var vcArray :Array<String> = Array()
            var NameArray :Array<String> = Array()
            var ImageArray :Array<String> = Array()
    
            for item  in objec{
                
                guard item[jsonControler] != nil&&item[jsontitle] != nil&&item[jsonimage] != nil else {
                    return defaultMainTabBar
                }
                
                vcArray.append(item[jsonControler] as! String)
                NameArray.append(item[jsontitle] as! String)
                ImageArray.append(item[jsonimage] as! String)
                
                
                
            }
            
            let JsonMainTabBar = JkmMainTabBarController(imageArray: ImageArray, TitleArray: NameArray, controllerArray: vcArray)
            
            return JsonMainTabBar
        
        }catch{
            JkLog("JSON解析异常")
            return defaultMainTabBar
        }
        

    }
    deinit{
        JkLog("VIewModel Died")
    }
    //MARK: 封装NetWorkTools中的方法
    /**
        封装NetWorkTools中的方法
     
     - parameter code:      Code码
     - parameter finished:  回调
     */
    func onlineGetUserMode(code :String,finished:(usermode:AnyObject?,error:NSError?)->()){
        let access_tokenString  = "https://api.weibo.com/oauth2/access_token" //获取access的地址
        let PostDic = ["client_id"     :appkey,
                       "client_secret" :appsecret,
                       "grant_type"    :"authorization_code",
                       "code"          :code,
                       "redirect_uri"  :uri]  //Post请求的字典
        
        let UserString = "https://api.weibo.com/2/users/show.json" //获取用户信息的 地址
        
        NetWorkTools.sharNetWorkTools.GetAcessTaken(access_tokenString, paramete: PostDic, UserURLString: UserString, Complete: { (usermode) in
                
                finished(usermode: usermode, error: nil)
            
            },WithError: { (errors) in
                
                finished(usermode: nil, error: errors)
            }
        )
        
    }


}